package com.jlkf.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.pojo.TblCourse;
import com.jlkf.pojo.TblProblem;
import com.jlkf.pojo.TblProfessionInfo;
import com.jlkf.pojo.TblProfessionSort;
import com.jlkf.pojo.TblSysUser;
import com.jlkf.pojo.TblUserWithdraw;
import com.jlkf.pojo.TblVipInfo;
import com.jlkf.service.CourseManagerService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;

@Controller
@RequestMapping("course")
public class CourseController {
	@Autowired
	private CourseManagerService courseManagerService;
	
	/**
	 * 一般课程
	 * @param startTime
	 * @param endTime
	 * @param type
	 * @param name
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("selectList")
	@ResponseBody
	public ReturnData selectList(String startTime,String endTime,Integer type,String name,Integer pageNumber,Integer pageSize){
		Map<String, Object> map = new HashMap<String,Object>();
		if(!StringTools.isEmpty(startTime)){
			map.put("startTime", startTime);
		}
		if(!StringTools.isEmpty(endTime)){
			map.put("endTime", endTime);
		}
		if(!StringTools.isEmpty(name)){
			map.put("name", name);
		}
		if(type!=null){
			map.put("type", type);
		}
		PageHelper.startPage(pageNumber, pageSize);
		List<TblCourse> list = courseManagerService.selectList(map);
		PageInfo<TblCourse> pageInfo = new PageInfo<>(list);
		return new ReturnData(list,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"一般课程列表");
	}
	
	/**
	 * 查看详情
	 */
	@RequestMapping("getGeneralCourseDetail")
	@ResponseBody
	public ModelAndView getGeneralCourseDetail(Long gId){
		TblCourse course = courseManagerService.selectDetail(gId);
		ModelAndView andView = new ModelAndView();
    	andView.addObject("course",course);
    	andView.setViewName("jsp/courseManager/generalDetail");
		return andView;
	}
	
	/**
	 * 设置折扣
	 */
	@RequestMapping("setGeneralCourseDiscont")
	@ResponseBody
	public ReturnData setGeneralCourseDiscont(Long gId,Integer discount){
	     Map<String, Object> map = new HashMap<String,Object>();
	     map.put("gId", gId);
	     map.put("discount", discount);
	     int num = courseManagerService.updateDiscount(map);
	     if(num>0){
	    	 return new ReturnData(200,"更新成功");
	     }else{
	    	 return new ReturnData(500,"更新失败");
	     }
	}
	
	/**
	 * 职业方向、知识体系、特色课程列表
	 * @param startTime
	 * @param endTime
	 * @param type
	 * @param name
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("selectOtherList")
	@ResponseBody
	public ReturnData selectOtherList(String startTime,String endTime,Integer type,String name,Integer pageNumber,Integer pageSize){
		Map<String, Object> map = new HashMap<String,Object>();
		map.put("type", type);
		if(!StringTools.isEmpty(startTime)){
			map.put("startTime", startTime);
		}
		if(!StringTools.isEmpty(endTime)){
			map.put("endTime", endTime);
		}
		if(!StringTools.isEmpty(name)){
			map.put("name", name);
		}
		PageHelper.startPage(pageNumber, pageSize);
		List<TblProfessionSort> list = courseManagerService.selectOtherList(map);
		PageInfo<TblProfessionSort> pageInfo = new PageInfo<>(list);
		return new ReturnData(list,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"职业方向、知识体系、特色课程列表");
	}
	
	/**
	 * 查看详情
	 */
	@RequestMapping("getOtherCourseDetail")
	@ResponseBody
	public ModelAndView getOtherCourseDetail(Integer gId){
		TblProfessionSort course = courseManagerService.selectOtherDetail(gId);
		ModelAndView andView = new ModelAndView();
    	andView.addObject("course",course);
    	andView.setViewName("jsp/courseManager/otherDetail");
		return andView;
	}
	
	/**
	 * 设置折扣
	 */
	@RequestMapping("setOtherCourseDiscont")
	@ResponseBody
	public ReturnData setOtherCourseDiscont(Integer gId,Integer discount){
	     Map<String, Object> map = new HashMap<String,Object>();
	     map.put("gId", gId);
	     map.put("discount", discount);
	     int num = courseManagerService.updateOtherDiscount(map);
	     if(num>0){
	    	 return new ReturnData(200,"更新成功");
	     }else{
	    	 return new ReturnData(500,"更新失败");
	     }
	}
	
	/**
	 * 会员列表
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("selectVipList")
	@ResponseBody
	public ReturnData selectOtherList(Integer pageNumber,Integer pageSize){
		PageHelper.startPage(pageNumber, pageSize);
		List<TblVipInfo> list = courseManagerService.vipList();
		PageInfo<TblVipInfo> pageInfo = new PageInfo<>(list);
		return new ReturnData(list,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"会员列表");
	}
	
	/**
	 * 查看详情
	 */
	@RequestMapping("tblVipDateil")
	@ResponseBody
	public ModelAndView tblVipDateil(Long gId){
		TblVipInfo vipInfo = courseManagerService.selectVipDetail(gId);
		ModelAndView andView = new ModelAndView();
    	andView.addObject("vipInfo",vipInfo);
    	andView.setViewName("jsp/courseManager/vipDiscountDetail");
		return andView;
	}
	
	/**
	 * 设置折扣
	 */
	@RequestMapping("setVipDiscont")
	@ResponseBody
	public ReturnData setVipDiscont(@RequestBody String json){
	     try{
	    	 TblVipInfo vipInfo = StringTools.json2Obj(json, TblVipInfo.class);
	    	 int num = courseManagerService.updateVipDiscount(vipInfo);
		     if(num>0){
		    	 return new ReturnData(200,"更新成功");
		     }else{
		    	 return new ReturnData(500,"更新失败");
		     }
	     }catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
	     }
	}
	
	/**
	 * 课程提问列表
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("getProblemList")
	@ResponseBody
	public ReturnData getProblemList(String courseName,Integer pageNumber,Integer pageSize){
		PageHelper.startPage(pageNumber, pageSize);
		List<TblProblem> list = courseManagerService.getProblemList(courseName);
		PageInfo<TblProblem> pageInfo = new PageInfo<>(list);
		return new ReturnData(list,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"课程提问列表");
	}
	
	/**
     *  置顶
     * @param courseId
     * @return
     */
    @RequestMapping("toTop")
    @ResponseBody
    public ReturnData toTop(Long gId){
  	 
  	   return courseManagerService.toTop(gId);
    }
    
    /**
     * 删除提问
     * @param courseId
     * @return
     */
    @RequestMapping("deleteProblem")
    @ResponseBody
    public ReturnData deleteProblem(Long gId){
       int num = courseManagerService.deleteProblem(gId);
       if(num>0){
      		 return new ReturnData(200,"删除成功");
 	   }else{
 		     return new ReturnData(500,"删除失败");
 	   }
    }
    
    /**
     * 是否在前端显示
     * @param courseId
     * @return
     */
    @RequestMapping("updateIsShow")
    @ResponseBody
    public ReturnData updateIsShow(Long gId,Integer isShow){
    	Map<String, Object> map = new HashMap<String,Object>();
    	map.put("gId", gId);
    	map.put("isShow", isShow);
        int num = courseManagerService.updateIsShow(map);
        if(num>0){
      		 return new ReturnData(200,"操作成功");
 	    }else{
 		     return new ReturnData(500,"操作失败");
 	    }
    }
    
    /**
     * 提问详情
     */
    @RequestMapping("problemDetail")
	@ResponseBody
	public ModelAndView problemDetail(Long gId){
		TblProblem problem = courseManagerService.getDetail(gId);
		ModelAndView andView = new ModelAndView();
    	andView.addObject("problem",problem);
    	andView.setViewName("jsp/courseManager/problemDetail");
		return andView;
	}
    
    
    /**
     * 提问详情(回复)
     */
    @RequestMapping("problemReply")
	@ResponseBody
	public ModelAndView problemReply(Long gId){
		TblProblem problem = courseManagerService.getDetail(gId);
		ModelAndView andView = new ModelAndView();
    	andView.addObject("problem",problem);
    	andView.setViewName("jsp/courseManager/problemReply");
		return andView;
	}
    
    /**
     * 提问回复
     * @param courseId
     * @return
     */
    @RequestMapping("updateProblem")
    @ResponseBody
    public ReturnData updateProblem(HttpSession session,Long gId,String replyContent){
    	TblSysUser user = (TblSysUser) session.getAttribute("sysUser");
    	TblProblem problem = new TblProblem();
    	problem.setgId(gId);
    	problem.setgReplyId((long)user.getgId());
    	problem.setgReplyContent(replyContent);
    	problem.setgReplyTime(new Date());
        int num = courseManagerService.updateProblem(problem);
        if(num>0){
      		 return new ReturnData(200,"回复成功");
 	    }else{
 		     return new ReturnData(500,"回复失败");
 	    }
    }
    
    /**
	 * 职业信息列表
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("getInfoList")
	@ResponseBody
	public ReturnData getInfoList(Integer pageNumber,Integer pageSize){
		PageHelper.startPage(pageNumber, pageSize);
		List<TblProfessionInfo> list = courseManagerService.getInfoList();
		PageInfo<TblProfessionInfo> pageInfo = new PageInfo<>(list);
		return new ReturnData(list,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"职业信息列表");
	}
	
	/**
	 * 新增或修改职业信息
	 */
	@RequestMapping("insertOrUpdate")
	@ResponseBody
	public ReturnData insertOrUpdate(@RequestBody String json){
		try{
			TblProfessionInfo info = StringTools.json2Obj(json, TblProfessionInfo.class);
			int num = 0;
			if(info.getgId()!=null){
				num = courseManagerService.update(info);
			}else{
				info.setgCreateTime(new Date());
				num = courseManagerService.insert(info);
			}
			if(num>0){
				return new ReturnData(200,"操作成功");
			}else{
				return new ReturnData(200,"操作失败");
			}
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"系统错误");
		}
	}
	
	/**
     * 职业信息详情
     */
    @RequestMapping("professionDetail")
	@ResponseBody
	public ModelAndView professionDetail(Integer gId){
		TblProfessionInfo info = courseManagerService.getInfoDetail(gId);
		ModelAndView andView = new ModelAndView();
    	andView.addObject("info",info);
    	andView.setViewName("jsp/courseManager/addOrUpdate");
		return andView;
	}
    
    /**
     * 删除职业信息
     */
    @RequestMapping("professionDelete")
	@ResponseBody
	public ReturnData professionDelete(Integer gId){
		int num = courseManagerService.deleteInfo(gId);
		if(num>0){
			return new ReturnData(200,"删除成功");
		}else{
			return new ReturnData(500,"删除失败");
		}
	}  
}
